<?php

namespace app\api\controller\haibao\jijin;

use app\admin\model\HaibaojijinLike;
use app\admin\model\HaibaojijinUser;
use app\admin\model\HaibaojijinVideo;
use app\admin\model\Xcx;
use app\api\controller\haibao\HaibaoBase;
use Pheanstalk\Pheanstalk;

/**
 * @author zero
 * @Time 2019/2/18
 *
 * Class User
 * @package app\api\controller\haibao\jijin
 *
 * 嗨爆集锦用户相关接口
 */
class User extends HaibaoBase
{
    // 登录接口
    public function login()
    {
        $gh_id = $this->request->post('gh_id');
        $code = $this->request->post('code');
        // 获取openid (微信服务器交互)
        $openid = $this->get_openid($gh_id, $code);
        if ($openid) {
            $user = HaibaojijinUser::get(['openid' => $openid]);
            if (!$user) {
                $user = new HaibaojijinUser();
                $user->openid = $openid;
                $user->is_formid = 1;
                $user->nick_name = null;
                $user->avatar_url = null;
            } else {
                $begin_today = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
                if ($user->prev_formid < $begin_today) {
                    $user->is_formid = 1;
                }
            }
            $user->save();
            $data['openid'] = $openid;
            $data['nick_name'] = $user->nick_name;
            $data['avatar_url'] = $user->avatar_url;
            $data['is_formid'] = $user->is_formid ? true : false;
            $this->result('ok', $data);
        } else {
            $this->error('fail');
        }
    }

    // 配置
    public function config()
    {
        $xcx = Xcx::get(['gh_id' => $this->gh_id]);
        $check = false;

        if ($xcx) {
            if ($this->version == $xcx->control_version) {
                $check = true;
            }
        }

        $this->result('config', ['check' => $check]);
    }

    // 更新玩家头像昵称
    public function update_user_info()
    {
        $nick_name = $this->request->post('nick_name');
        $avatar_url = $this->request->post('avatar_url');
        if ($nick_name && $nick_name !== '') {
            $this->user->nick_name = $nick_name;
        }
        if ($avatar_url && $avatar_url !== '') {
            $this->user->avatar_url = $avatar_url;
        }
        $this->user->save();
        $this->result('ok');
    }

    // 记录用户分享视频
    public function handle_share()
    {
        $video_id = $this->request->param('video_id');

        /**
         * TODO:
         * 用户相关操作
         * 经验值增加
         */

        // 视频分享统计
        $video = HaibaojijinVideo::get($video_id);
        $video->share_num += 1;
        $video->save();
        $this->result('ok');
    }

    // 处理点赞
    public function handle_like()
    {
        $like_id = $this->request->post('video_id');

        if ($like_id) {
            $like = HaibaojijinLike::get(['user_id' => $this->user->id, 'like_id' => $like_id]);

            if (!$like) {
                $like = new HaibaojijinLike();
                $like->user_id = $this->user->id;
                $like->like_id = $like_id;
            } else {
                if ($like->delstatus == 1) {
                    $like->delstatus = 0;
                } elseif ($like->delstatus == 0) {
                    $like->delstatus = 1;
                }
            }
            $like->save();
            $this->result('ok');
        }
        $this->result('fail');
    }

    // 获取点赞列表
    public function get_like_list()
    {
        $favor_list = HaibaojijinLike::where('delstatus', 0)
            ->where('user_id', $this->user->id)
            ->column('like_id');
        $list = [];
        foreach ($favor_list as $like_id) {
            $item = HaibaojijinVideo::get($like_id);
            if ($item) {
                $list[] = $item;
            }
        }
        $this->result('list', $list);
    }

    // 处理收到formid,并创建发送任务
    public function handler_formid()
    {
//        // 模板消息数据
//        $tmp = array(
//            'template_id' => '0PCXpAXpejvUFRSvQ7i6xjoBbWv0xpAQfeDnpZFNbjo',
//            'page' => 'pages/index/index',
//            'data' => array(
//                'keyword1' => '惊！女主播居然…',
//                'keyword2' => '点击进入小程序,查看详细信息',
//            )
//        );
//        $ghid = $this->request->post('ghid');
//        $openid = $this->request->post('openid');
//        $formid = $this->request->post('formid');
//
//        if ($formid) {
//            $acc = HaibaoAcc::get(['openid' => $openid]);
//            $acc->is_formid = 0;
//            $acc->prev_formid = time();
//            $acc->save();
//
//            $xcx = Xcx::get(['ghid' => $ghid]);
//
//            $data['appid'] = $xcx->appid;
//            $data['secret'] = $xcx->secret;
//            $data['openid'] = $openid;
//            $data['formid'] = $formid;
//            $data['tmp'] = $tmp;
//
//            $ser = new Pheanstalk('127.0.0.1');
//            if ($ser->useTube('xcx_tmp')->put(json_encode($data), 2, 86000, 60)) {
//                $this->success('ok');
//            } else {
//                $this->error('error');
//            }
//        }
    }
}
